package com.esms.struts.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;

import com.esms.bean.Employee;
import com.esms.bean.Enterprise;
import com.esms.bean.EnterpriseDAO;
import com.esms.bean.Salaryserviceemployee;

public class EnterpriseService {
	private static Logger log = Logger.getLogger(EnterpriseService.class);
	
	private EnterpriseService() {}
	public static EnterpriseService $ = new EnterpriseService(); 
	
	public List getAllEnterprises() {
		return EnterpriseDAO.$.findAll();
	}

	//cMap<cid,eMap>
	//eMap<eid,sList>
	//即为cMap<cid, <eid,sList>>
	public Map getAllEnterprises(String person) {
		List enterprises = new ArrayList();
		enterprises = EnterpriseDAO.$.findByPersonid(PersonService.$.getPersonId(person));
		Map cMap = new HashMap();
		for(int i = 0; i < enterprises.size(); i++) {
			Enterprise enterprise = (Enterprise) enterprises.get(i);
			Set employees = enterprise.getEmployees();
			Map eMap = new HashMap();
			for(Iterator it = employees.iterator(); it.hasNext(); ) {
				Employee employee = (Employee) it.next();
				if(EmployeeService.$.existSalaryService(employee.getEmployeeId())) {
					Salaryserviceemployee[] salaryServices = EmployeeService.$.getSalaryService(employee.getEmployeeId());
					List sList = new ArrayList();
					for(int j = 0; j < salaryServices.length; j++) {
						sList.add(salaryServices[j].getId());
					}
					eMap.put(employee.getEmployeeId(), sList);
				}
			}
			cMap.put(enterprise.getEnterpriseId(), eMap);
		}		
		return cMap;
	}
	
	public List getEnterpriseList(Map cMap) {
		Set set = cMap.keySet();
		List list = new ArrayList();
		list.addAll(set);
		return list;
	}
	
	public List getEmployeeList(Map cMap) {
		Set set = cMap.keySet();
		List list = new ArrayList();
		for(Iterator it = set.iterator(); it.hasNext(); ) {
			Map eMap = (HashMap) cMap.get(it.next());
			Set temp = eMap.keySet();
			list.addAll(temp); 
		}
		return list;
	}
	
	public List getServiceList(Map cMap) {
		Set set = cMap.keySet();
		List list = new ArrayList();
		for(Iterator it = set.iterator(); it.hasNext(); ) {
			Map eMap = (HashMap) cMap.get(it.next());
			Set temp = cMap.keySet();
			for(Iterator iterator = temp.iterator(); iterator.hasNext(); ) {
				List tempList = (List) eMap.get(iterator.next());
				if(tempList != null)
					list.addAll(tempList); 
			}
		}
		return list;
	}
	
	public void createNewEnterprise(Enterprise enterprise) {
		EnterpriseDAO.$.insert(enterprise);
	}
	
	public Enterprise getEnterpriseById(Integer enterprise_id) {
		Enterprise enterprise = EnterpriseDAO.$.findById(enterprise_id);
		return enterprise;
	}

	public Enterprise getEnterpriseById(int enterpriseId) {
		return EnterpriseDAO.$.findById(enterpriseId);
	}
	
	public List getAllEmployees(Enterprise enterprise) {
		Set set = enterprise.getEmployees();
		List list = new ArrayList();
		list.addAll(set);
		System.out.println(set.isEmpty() ? 100 : set.size());
		/*
		log.info(set + "0--------------------");
		if(set.isEmpty()) 
			return new ArrayList(0);
		List list = new ArrayList();
		for(Iterator it = (Iterator) set.iterator(); it.hasNext();) {
			list.add(it.next());
		}
		*/
		return list;
	}
}
